<?php
	require("libraries/student.php");
	class Questions extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_question");
			$this->load->library("string");
			$this->load->library("form_validation");
			$this->load->database();
		}
		public function index(){
			$config['base_url'] = base_url().'admin/questions/index/';
			$config['total_rows'] = $this->model_question->countall();
			$data['title'] = "Questions";
			$data['template'] = "questions/list";
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";
			$data['act'] = 4;
			$data['info']= $this->model_question->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function add(){
			$data['error'] = "";
			$data['data']  = "";
			$data['act'] = 4;
			$data['title'] = "Add new question";
			$data['template'] = "questions/add";
			$data['listcate'] = $this->model_question->listcate();
			if($this->input->post("okie") != ""){
				$title = trim($this->input->post('title'));
				$url = str_replace(' ', '-',strtolower($this->string->replace($title)));
				$db = array(
					"title" => $this->input->post("title"),
					"rewrite" => $url,
					"keywords" => $this->input->post("keywords"),
					"description" => $this->input->post("description"),
					"info" => $this->input->post("info"),
					"views" => $this->input->post("views"),
					"likes" => $this->input->post("likes"),
					"dislike" => $this->input->post("dislike"),
					"username" => $this->input->post("username"),
					"userid" => $this->input->post("userid"),
					"cagoid" => $this->input->post("cagoid"),
					"cateid" => $this->input->post("cateid")
				);
				if($_FILES['image']['name'] != NULL){
					$config['upload_path'] = './uploads/questions';
					$config['allowed_types'] = 'jpg|jpeg|gif|png';
					$config['max_size']	= '2000';
					$config['max_width']  = '1024';
					$config['max_height']  = '1000';
					$this->load->library('upload',$config);
					if(!$this->upload->do_upload("image")){
						$data = array('error' => $this->upload->display_errors());
						$data['data']  = "";$data['act'] = 4;
						$data['title'] = "Add new question";
			  			$data['template'] = "questions/add";
						$data['listcate'] = $this->model_question->listcate();
						$this->load->view("layout",$data);
						return FALSE;
					}else{
						$data = $this->upload->data();
						$db['image'] = $data['file_name'];
						$this->createThumbnail($db['image'],"150","130");
					}
				}
				$this->model_question->add($db);
				redirect(base_url()."admin/questions");	
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['error'] = "";$data['act'] = 4;
			$data['title'] = "Edit a question";
			$data['template'] = "questions/edit";
			$data['result'] = $this->model_question->getdata($id);
			$data['listcate'] = $this->model_question->listcate();
			$data['data'] = "vl";
			if($this->input->post("okie") != ""){
				$title = trim($this->input->post('title'));
				$url = str_replace(' ', '-',strtolower($this->string->replace($title)));
				$db = array(
					"title" => $this->input->post("title"),
					"rewrite" => $url,
					"keywords" => $this->input->post("keywords"),
					"description" => $this->input->post("description"),
					"info" => $this->input->post("info"),
					"views" => $this->input->post("views"),
					"likes" => $this->input->post("likes"),
					"dislike" => $this->input->post("dislike"),
					"username" => $this->input->post("username"),
					"userid" => $this->input->post("userid"),
					"cagoid" => $this->input->post("cagoid"),
					"cateid" => $this->input->post("cateid")
				);
				if($_FILES['image']['name'] != NULL){
					$config['upload_path'] = './uploads/questions';
					$config['allowed_types'] = 'jpg|jpeg|gif|png';
					$config['max_size']	= '2000';
					$config['max_width']  = '1024';
					$config['max_height']  = '1000';
					$this->load->library('upload',$config);
					if(!$this->upload->do_upload("image")){
						$data = array('error' => $this->upload->display_errors());
						$data['data']  = "";$data['act'] = 4;
						$data['result'] = $this->model_question->getdata($id);
						$data['listcate'] = $this->model_question->listcate();
						$data['title'] = "Edit a question";
			  			$data['template'] = "questions/edit";
						$this->load->view("layout",$data);
						return FALSE;
					}else{
						$data = $this->upload->data();
						$db['image'] = $data['file_name'];
						$this->createThumbnail($db['image'],"150","130");
					}
				}
				$this->model_question->update($id,$db);
				redirect(base_url()."admin/questions");	
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function getcago(){
			$id = $_POST['id'];
			$data = $this->model_question->listcago($id);
			if($data != NULL){
				echo '<select name="cagoid">';
                foreach($data as $items){
					echo "<option value='".$items['id']."'>".$items['name']."</option>";
				}
                echo "</select>";
			}else{
				echo "No data";
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$data = $this->model_question->getdata($id);
			@unlink("uploads/questions/".$data['image']);
			@unlink("uploads/questions/thumb/".$data['image']);
			$this->model_question->del($id);
			redirect(base_url()."admin/questions");
		}
		public function createThumbnail($fileName,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/questions/'.$fileName;
			$config['new_image'] = 'uploads/questions/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}